<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Qt 4.3: canvas.cpp Example File (graphicsview/portedcanvas/canvas.cpp)</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">canvas.cpp Example File<br /><sup><sup>graphicsview/portedcanvas/canvas.cpp</sup></sup></h1>
<pre><span class="comment"> /****************************************************************************
 **
 ** Copyright (C) 2006-2008 Trolltech ASA. All rights reserved.
 **
 ** This file is part of the documentation of the Qt Toolkit.
 **
 ** This file may be used under the terms of the GNU General Public
** License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file.  Alternatively you may (at
** your option) use any later version of the GNU General Public
** License if such license has been publicly approved by Trolltech ASA
** (or its successors, if any) and the KDE Free Qt Foundation. 
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/. If
** you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** In addition, as a special exception, Trolltech, as the sole
** copyright holder for Qt Designer, grants users of the Qt/Eclipse
** Integration plug-in the right for the Qt/Eclipse Integration to
** link to functionality provided by Qt Designer and its related
** libraries.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly
** granted herein.
 **
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 **
 ****************************************************************************/</span>

 #include &lt;qdatetime.h&gt;
 #include &lt;qmainwindow.h&gt;
 #include &lt;qstatusbar.h&gt;
 #include &lt;qmessagebox.h&gt;
 #include &lt;qmenubar.h&gt;
 #include &lt;qapplication.h&gt;
 #include &lt;qpainter.h&gt;
 #include &lt;qprinter.h&gt;
 #include &lt;qlabel.h&gt;
 #include &lt;qimage.h&gt;
 #include &lt;q3progressdialog.h&gt;
 #include &lt;Q3PointArray&gt;
 #include &lt;Q3PtrList&gt;
 #include &lt;QPixmap&gt;
 #include &lt;Q3PopupMenu&gt;
 #include &lt;QMouseEvent&gt;
 #include &lt;Q3MemArray&gt;
 #include &quot;canvas.h&quot;
 #include &lt;QStyleOptionGraphicsItem&gt;
 #include &lt;qdebug.h&gt;
 #include &lt;stdlib.h&gt;

<span class="comment"> //</span> We use a global variable to save memory - all the brushes and pens in
<span class="comment"> //</span> the mesh are shared.
 static QBrush *tb = 0;
 static QPen *tp = 0;

 class EdgeItem;
 class NodeItem;

 class EdgeItem: public QGraphicsLineItem
 {
 public:
     EdgeItem( NodeItem*, NodeItem* );
     void setFromPoint( int x, int y ) ;
     void setToPoint( int x, int y );
     static int count() { return c; }
 private:
     static int c;
 };

 static const int imageRTTI = 984376;

 class ImageItem: public QGraphicsRectItem
 {
 public:
     ImageItem( QImage img );
     int rtti () const { return imageRTTI; }
 protected:
     void paint( QPainter *, const QStyleOptionGraphicsItem *option, QWidget *widget );
 private:
     QImage image;
     QPixmap pixmap;
 };

 ImageItem::ImageItem( QImage img )
     : image(img)
 {
     setRect(0, 0, image.width(), image.height());
     setFlag(ItemIsMovable);
 #if !defined(Q_WS_QWS)
     pixmap.convertFromImage(image, Qt::OrderedAlphaDither);
 #endif
 }

 void ImageItem::paint( QPainter *p, const QStyleOptionGraphicsItem *option, QWidget * )
 {
<span class="comment"> //</span> On Qt/Embedded, we can paint a QImage as fast as a QPixmap,
<span class="comment"> //</span> but on other platforms, we need to use a QPixmap.
 #if defined(Q_WS_QWS)
     p-&gt;drawImage( option-&gt;exposedRect, image, option-&gt;exposedRect, Qt::OrderedAlphaDither );
 #else
     p-&gt;drawPixmap( option-&gt;exposedRect, pixmap, option-&gt;exposedRect );
 #endif
 }

 class NodeItem: public QGraphicsEllipseItem
 {
 public:
     NodeItem();
     ~NodeItem() {}

     void addInEdge( EdgeItem *edge ) { inList.append( edge ); }
     void addOutEdge( EdgeItem *edge ) { outList.append( edge ); }

 protected:
     QVariant itemChange(GraphicsItemChange change, const QVariant &amp;value);

     <span class="comment">//</span>    QPoint center() { return boundingRect().center(); }
 private:
     Q3PtrList&lt;EdgeItem&gt; inList;
     Q3PtrList&lt;EdgeItem&gt; outList;
 };

 int EdgeItem::c = 0;

 EdgeItem::EdgeItem( NodeItem *from, NodeItem *to )
     : QGraphicsLineItem( )
 {
     c++;
     setPen( *tp );
     from-&gt;addOutEdge( this );
     to-&gt;addInEdge( this );
     setLine( QLineF(int(from-&gt;x()), int(from-&gt;y()), int(to-&gt;x()), int(to-&gt;y()) ));
     setZValue( 127 );
 }

 void EdgeItem::setFromPoint( int x, int y )
 {
     setLine(QLineF( x,y, line().p2().x(), line().p2().y() ));
 }

 void EdgeItem::setToPoint( int x, int y )
 {
     setLine(QLineF( line().p1().x(), line().p1().y(), x, y ));
 }

 QVariant NodeItem::itemChange(GraphicsItemChange change, const QVariant &amp;value)
 {
     if (change == ItemPositionHasChanged) {
         Q3PtrListIterator&lt;EdgeItem&gt; it1( inList );
         EdgeItem *edge;
         while (( edge = it1.current() )) {
             ++it1;
             edge-&gt;setToPoint( int(x()), int(y()) );
         }
         Q3PtrListIterator&lt;EdgeItem&gt; it2( outList );
         while (( edge = it2.current() )) {
             ++it2;
             edge-&gt;setFromPoint( int(x()), int(y()) );
         }
     }

     return QGraphicsEllipseItem::itemChange(change, value);
 }

 NodeItem::NodeItem( )
     : QGraphicsEllipseItem( QRectF(-3, -3, 6, 6) )
 {
     setPen( *tp );
     setBrush( *tb );
     setZValue( 128 );
     setFlag(ItemIsMovable);
 }

 FigureEditor::FigureEditor(
         QGraphicsScene&amp; c, QWidget* parent,
         const char* name, Qt::WindowFlags f) :
     QGraphicsView(&amp;c,parent)
 {
     setObjectName(name);
     setWindowFlags(f);
     setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
 }

 void FigureEditor::clear()
 {
     QList&lt;QGraphicsItem *&gt; list = scene()-&gt;items();
     QList&lt;QGraphicsItem *&gt;::Iterator it = list.begin();
     for (; it != list.end(); ++it) {
         if ( *it )
             delete *it;
     }
 }

 BouncyLogo::BouncyLogo() :
     xvel(0), yvel(0)
 {
     setPixmap(QPixmap(&quot;:/trolltech/examples/graphicsview/portedcanvas/qt-trans.xpm&quot;));
 }

 const int logo_rtti = 1234;

 int BouncyLogo::type() const
 {
     return logo_rtti;
 }

 QPainterPath BouncyLogo::shape() const
 {
     QPainterPath path;
     path.addRect(boundingRect());
     return path;
 }

 void BouncyLogo::initPos()
 {
     initSpeed();
     int trial=1000;
     do {
         setPos(qrand()%int(scene()-&gt;width()),qrand()%int(scene()-&gt;height()));
         advance(0);
     } while (trial-- &amp;&amp; xvel==0.0 &amp;&amp; yvel==0.0);
 }

 void BouncyLogo::initSpeed()
 {
     const double speed = 4.0;
     double d = (double)(qrand()%1024) / 1024.0;
     xvel = d*speed*2-speed;
     yvel = (1-d)*speed*2-speed;
 }

 void BouncyLogo::advance(int stage)
 {
     switch ( stage ) {
       case 0: {
         double vx = xvel;
         double vy = yvel;

         if ( vx == 0.0 &amp;&amp; vy == 0.0 ) {
             <span class="comment">//</span> stopped last turn
             initSpeed();
             vx = xvel;
             vy = yvel;
         }

         double nx = x() + vx;
         double ny = y() + vy;

         if ( nx &lt; 0 || nx &gt;= scene()-&gt;width() )
             vx = -vx;
         if ( ny &lt; 0 || ny &gt;= scene()-&gt;height() )
             vy = -vy;

         for (int bounce=0; bounce&lt;4; bounce++) {
             QList&lt;QGraphicsItem *&gt; l=scene()-&gt;collidingItems(this);
             for (QList&lt;QGraphicsItem *&gt;::Iterator it=l.begin(); it!=l.end(); ++it) {
                 QGraphicsItem *hit = *it;
                 QPainterPath advancedShape = QMatrix().translate(xvel, yvel).map(shape());
                 if ( hit-&gt;type()==logo_rtti &amp;&amp; hit-&gt;collidesWithPath(mapToItem(hit, advancedShape)) ) {
                     switch ( bounce ) {
                       case 0:
                         vx = -vx;
                         break;
                       case 1:
                         vy = -vy;
                         vx = -vx;
                         break;
                       case 2:
                         vx = -vx;
                         break;
                       case 3:
                         <span class="comment">//</span> Stop for this turn
                         vx = 0;
                         vy = 0;
                         break;
                     }
                     xvel = vx;
                     yvel = vy;
                     break;
                 }
             }
         }

         if ( x()+vx &lt; 0 || x()+vx &gt;= scene()-&gt;width() )
             vx = 0;
         if ( y()+vy &lt; 0 || y()+vy &gt;= scene()-&gt;height() )
             vy = 0;

         xvel = vx;
         yvel = vy;
       } break;
       case 1:
         moveBy(xvel, yvel);
         break;
     }
 }

 static uint mainCount = 0;
 static QImage *butterflyimg;
 static QImage *logoimg;

 Main::Main(QGraphicsScene&amp; c, QWidget* parent, const char* name, Qt::WindowFlags f) :
     Q3MainWindow(parent,name,f),
     canvas(c)
 {
     editor = new FigureEditor(canvas,this);
     QMenuBar* menu = menuBar();

     Q3PopupMenu* file = new Q3PopupMenu( menu );
     file-&gt;insertItem(&quot;&amp;Fill canvas&quot;, this, SLOT(init()), Qt::CTRL+Qt::Key_F);
     file-&gt;insertItem(&quot;&amp;Erase canvas&quot;, this, SLOT(clear()), Qt::CTRL+Qt::Key_E);
     file-&gt;insertItem(&quot;&amp;New view&quot;, this, SLOT(newView()), Qt::CTRL+Qt::Key_N);
     file-&gt;insertSeparator();
     file-&gt;insertItem(&quot;&amp;Print...&quot;, this, SLOT(print()), Qt::CTRL+Qt::Key_P);
     file-&gt;insertSeparator();
     file-&gt;insertItem(&quot;E&amp;xit&quot;, qApp, SLOT(quit()), Qt::CTRL+Qt::Key_Q);
     menu-&gt;insertItem(&quot;&amp;File&quot;, file);

     Q3PopupMenu* edit = new Q3PopupMenu( menu );
     edit-&gt;insertItem(&quot;Add &amp;Circle&quot;, this, SLOT(addCircle()), Qt::ALT+Qt::Key_C);
     edit-&gt;insertItem(&quot;Add &amp;Hexagon&quot;, this, SLOT(addHexagon()), Qt::ALT+Qt::Key_H);
     edit-&gt;insertItem(&quot;Add &amp;Polygon&quot;, this, SLOT(addPolygon()), Qt::ALT+Qt::Key_P);
     edit-&gt;insertItem(&quot;Add Spl&amp;ine&quot;, this, SLOT(addSpline()), Qt::ALT+Qt::Key_I);
     edit-&gt;insertItem(&quot;Add &amp;Text&quot;, this, SLOT(addText()), Qt::ALT+Qt::Key_T);
     edit-&gt;insertItem(&quot;Add &amp;Line&quot;, this, SLOT(addLine()), Qt::ALT+Qt::Key_L);
     edit-&gt;insertItem(&quot;Add &amp;Rectangle&quot;, this, SLOT(addRectangle()), Qt::ALT+Qt::Key_R);
     edit-&gt;insertItem(&quot;Add &amp;Sprite&quot;, this, SLOT(addSprite()), Qt::ALT+Qt::Key_S);
     edit-&gt;insertItem(&quot;Create &amp;Mesh&quot;, this, SLOT(addMesh()), Qt::ALT+Qt::Key_M );
     edit-&gt;insertItem(&quot;Add &amp;Alpha-blended image&quot;, this, SLOT(addButterfly()), Qt::ALT+Qt::Key_A);
     menu-&gt;insertItem(&quot;&amp;Edit&quot;, edit);

     Q3PopupMenu* view = new Q3PopupMenu( menu );
     view-&gt;insertItem(&quot;&amp;Enlarge&quot;, this, SLOT(enlarge()), Qt::SHIFT+Qt::CTRL+Qt::Key_Plus);
     view-&gt;insertItem(&quot;Shr&amp;ink&quot;, this, SLOT(shrink()), Qt::SHIFT+Qt::CTRL+Qt::Key_Minus);
     view-&gt;insertSeparator();
     view-&gt;insertItem(&quot;&amp;Rotate clockwise&quot;, this, SLOT(rotateClockwise()), Qt::CTRL+Qt::Key_PageDown);
     view-&gt;insertItem(&quot;Rotate &amp;counterclockwise&quot;, this, SLOT(rotateCounterClockwise()), Qt::CTRL+Qt::Key_PageUp);
     view-&gt;insertItem(&quot;&amp;Zoom in&quot;, this, SLOT(zoomIn()), Qt::CTRL+Qt::Key_Plus);
     view-&gt;insertItem(&quot;Zoom &amp;out&quot;, this, SLOT(zoomOut()), Qt::CTRL+Qt::Key_Minus);
     view-&gt;insertItem(&quot;Translate left&quot;, this, SLOT(moveL()), Qt::CTRL+Qt::Key_Left);
     view-&gt;insertItem(&quot;Translate right&quot;, this, SLOT(moveR()), Qt::CTRL+Qt::Key_Right);
     view-&gt;insertItem(&quot;Translate up&quot;, this, SLOT(moveU()), Qt::CTRL+Qt::Key_Up);
     view-&gt;insertItem(&quot;Translate down&quot;, this, SLOT(moveD()), Qt::CTRL+Qt::Key_Down);
     view-&gt;insertItem(&quot;&amp;Mirror&quot;, this, SLOT(mirror()), Qt::CTRL+Qt::Key_Home);
     menu-&gt;insertItem(&quot;&amp;View&quot;, view);

     menu-&gt;insertSeparator();

     Q3PopupMenu* help = new Q3PopupMenu( menu );
     help-&gt;insertItem(&quot;&amp;About&quot;, this, SLOT(help()), Qt::Key_F1);
     help-&gt;setItemChecked(dbf_id, TRUE);
     menu-&gt;insertItem(&quot;&amp;Help&quot;,help);

     statusBar();

     setCentralWidget(editor);

     printer = 0;

     init();
 }

 void Main::init()
 {
     clear();

     static int r=24;
     qsrand(++r);

     mainCount++;
     butterflyimg = 0;
     logoimg = 0;

     int i;
     for ( i=0; i &lt; int(canvas.width()) / 56; i++) {
         addButterfly();
     }
     for ( i=0; i &lt; int(canvas.width()) / 85; i++) {
         addHexagon();
     }
     for ( i=0; i &lt; int(canvas.width()) / 128; i++) {
         addLogo();
     }
 }

 Main::~Main()
 {
     delete printer;
     if ( !--mainCount ) {
         delete[] butterflyimg;
         butterflyimg = 0;
         delete[] logoimg;
         logoimg = 0;
     }
 }

 void Main::newView()
 {
     <span class="comment">//</span> Open a new view... have it delete when closed.
     Main *m = new Main(canvas, 0, 0, Qt::WDestructiveClose);
     m-&gt;show();
 }

 void Main::clear()
 {
     editor-&gt;clear();
 }

 void Main::help()
 {
     static QMessageBox* about = new QMessageBox( &quot;Qt Canvas Example&quot;,
             &quot;&lt;h3&gt;The QCanvas classes example&lt;/h3&gt;&quot;
             &quot;&lt;ul&gt;&quot;
                 &quot;&lt;li&gt; Press ALT-S for some sprites.&quot;
                 &quot;&lt;li&gt; Press ALT-C for some circles.&quot;
                 &quot;&lt;li&gt; Press ALT-L for some lines.&quot;
                 &quot;&lt;li&gt; Drag the objects around.&quot;
                 &quot;&lt;li&gt; Read the code!&quot;
             &quot;&lt;/ul&gt;&quot;, QMessageBox::Information, 1, 0, 0, this, 0, FALSE );
     about-&gt;setButtonText( 1, &quot;Dismiss&quot; );
     about-&gt;show();
 }

 void Main::aboutQt()
 {
     QMessageBox::aboutQt( this, &quot;Qt Canvas Example&quot; );
 }

 void Main::enlarge()
 {
     canvas.setSceneRect(0, 0, canvas.width()*4/3, canvas.height()*4/3);
 }

 void Main::shrink()
 {
     canvas.setSceneRect(0, 0, qMax(canvas.width()*3/4, qreal(1.0)), qMax(canvas.height()*3/4, qreal(1.0)));
 }

 void Main::rotateClockwise()
 {
     editor-&gt;rotate( 22.5 );
 }

 void Main::rotateCounterClockwise()
 {
     editor-&gt;rotate( -22.5 );
 }

 void Main::zoomIn()
 {
     editor-&gt;scale( 2.0, 2.0 );
 }

 void Main::zoomOut()
 {
     editor-&gt;scale( 0.5, 0.5 );
 }

 void Main::mirror()
 {
     editor-&gt;scale( -1, 1 );
 }

 void Main::moveL()
 {
     editor-&gt;translate( -16, 0 );
 }

 void Main::moveR()
 {
     editor-&gt;translate( +16, 0 );
 }

 void Main::moveU()
 {
     editor-&gt;translate( 0, -16 );
 }

 void Main::moveD()
 {
     editor-&gt;translate( 0, +16 );
 }

 void Main::print()
 {
     if ( !printer ) printer = new QPrinter;
     if ( printer-&gt;setup(this) ) {
         QPainter pp(printer);
         canvas.render(&amp;pp);
     }
 }

 void Main::addSprite()
 {
     BouncyLogo* i = new BouncyLogo;
     canvas.addItem(i);
     i-&gt;initPos();
     i-&gt;setZValue(qrand()%256);
 }

 QString butterfly_fn;
 QString logo_fn;

 void Main::addButterfly()
 {
     if ( butterfly_fn.isEmpty() )
         return;
     if ( !butterflyimg ) {
         butterflyimg = new QImage[4];
         butterflyimg[0].load( butterfly_fn );
         butterflyimg[1] = butterflyimg[0].smoothScale( int(butterflyimg[0].width()*0.75),
                 int(butterflyimg[0].height()*0.75) );
         butterflyimg[2] = butterflyimg[0].smoothScale( int(butterflyimg[0].width()*0.5),
                 int(butterflyimg[0].height()*0.5) );
         butterflyimg[3] = butterflyimg[0].smoothScale( int(butterflyimg[0].width()*0.25),
                 int(butterflyimg[0].height()*0.25) );
     }
     QAbstractGraphicsShapeItem* i = new ImageItem(butterflyimg[qrand()%4]);
     canvas.addItem(i);
     i-&gt;setPos(qrand()%int(canvas.width()-butterflyimg-&gt;width()),
             qrand()%int(canvas.height()-butterflyimg-&gt;height()));
     i-&gt;setZValue(qrand()%256+250);
 }

 void Main::addLogo()
 {
     if ( logo_fn.isEmpty() )
         return;
     if ( !logoimg ) {
         logoimg = new QImage[4];
         logoimg[0].load( logo_fn );
         logoimg[1] = logoimg[0].smoothScale( int(logoimg[0].width()*0.75),
                 int(logoimg[0].height()*0.75) );
         logoimg[2] = logoimg[0].smoothScale( int(logoimg[0].width()*0.5),
                 int(logoimg[0].height()*0.5) );
         logoimg[3] = logoimg[0].smoothScale( int(logoimg[0].width()*0.25),
                 int(logoimg[0].height()*0.25) );
     }
     QAbstractGraphicsShapeItem* i = new ImageItem(logoimg[qrand()%4]);
     canvas.addItem(i);
     i-&gt;setPos(qrand()%int(canvas.width()-logoimg-&gt;width()),
             qrand()%int(canvas.height()-logoimg-&gt;width()));
     i-&gt;setZValue(qrand()%256+256);
 }

 void Main::addCircle()
 {
     QAbstractGraphicsShapeItem* i = canvas.addEllipse(QRectF(0,0,50,50));
     i-&gt;setFlag(QGraphicsItem::ItemIsMovable);
     i-&gt;setPen(Qt::NoPen);
     i-&gt;setBrush( QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8) );
     i-&gt;setPos(qrand()%int(canvas.width()),qrand()%int(canvas.height()));
     i-&gt;setZValue(qrand()%256);
 }

 void Main::addHexagon()
 {
     const int size = int(canvas.width() / 25);
     Q3PointArray pa(6);
     pa[0] = QPoint(2*size,0);
     pa[1] = QPoint(size,-size*173/100);
     pa[2] = QPoint(-size,-size*173/100);
     pa[3] = QPoint(-2*size,0);
     pa[4] = QPoint(-size,size*173/100);
     pa[5] = QPoint(size,size*173/100);
     QGraphicsPolygonItem* i = canvas.addPolygon(pa);
     i-&gt;setFlag(QGraphicsItem::ItemIsMovable);
     i-&gt;setPen(Qt::NoPen);
     i-&gt;setBrush( QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8) );
     i-&gt;setPos(qrand()%int(canvas.width()),qrand()%int(canvas.height()));
     i-&gt;setZValue(qrand()%256);
 }

 void Main::addPolygon()
 {
     const int size = int(canvas.width()/2);
     Q3PointArray pa(6);
     pa[0] = QPoint(0,0);
     pa[1] = QPoint(size,size/5);
     pa[2] = QPoint(size*4/5,size);
     pa[3] = QPoint(size/6,size*5/4);
     pa[4] = QPoint(size*3/4,size*3/4);
     pa[5] = QPoint(size*3/4,size/4);
     QGraphicsPolygonItem* i = canvas.addPolygon(pa);
     i-&gt;setFlag(QGraphicsItem::ItemIsMovable);
     i-&gt;setPen(Qt::NoPen);
     i-&gt;setBrush( QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8) );
     i-&gt;setPos(qrand()%int(canvas.width()),qrand()%int(canvas.height()));
     i-&gt;setZValue(qrand()%256);
 }

 void Main::addSpline()
 {
     const int size = int(canvas.width()/6);

     Q3PointArray pa(12);
     pa[0] = QPoint(0,0);
     pa[1] = QPoint(size/2,0);
     pa[2] = QPoint(size,size/2);
     pa[3] = QPoint(size,size);
     pa[4] = QPoint(size,size*3/2);
     pa[5] = QPoint(size/2,size*2);
     pa[6] = QPoint(0,size*2);
     pa[7] = QPoint(-size/2,size*2);
     pa[8] = QPoint(size/4,size*3/2);
     pa[9] = QPoint(0,size);
     pa[10]= QPoint(-size/4,size/2);
     pa[11]= QPoint(-size/2,0);

     QPainterPath path;
     path.moveTo(pa[0]);
     for (int i = 1; i &lt; pa.size(); i += 3)
         path.cubicTo(pa[i], pa[(i + 1) % pa.size()], pa[(i + 2) % pa.size()]);

     QGraphicsPathItem* item = canvas.addPath(path);
     item-&gt;setFlag(QGraphicsItem::ItemIsMovable);
     item-&gt;setPen(Qt::NoPen);
     item-&gt;setBrush( QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8) );
     item-&gt;setPos(qrand()%int(canvas.width()),qrand()%int(canvas.height()));
     item-&gt;setZValue(qrand()%256);
 }

 void Main::addText()
 {
     QGraphicsTextItem* i = canvas.addText(&quot;QCanvasText&quot;);
     i-&gt;setFlag(QGraphicsItem::ItemIsMovable);
     i-&gt;setPos(qrand()%int(canvas.width()),qrand()%int(canvas.height()));
     i-&gt;setZValue(qrand()%256);
 }

 void Main::addLine()
 {
     QGraphicsLineItem* i = canvas.addLine(QLineF( qrand()%int(canvas.width()), qrand()%int(canvas.height()),
                                                   qrand()%int(canvas.width()), qrand()%int(canvas.height()) ));
     i-&gt;setFlag(QGraphicsItem::ItemIsMovable);
     i-&gt;setPen( QPen(QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8), 6) );
     i-&gt;setZValue(qrand()%256);
 }

 void Main::addMesh()
 {
     int x0 = 0;
     int y0 = 0;

     if ( !tb ) tb = new QBrush( Qt::red );
     if ( !tp ) tp = new QPen( Qt::black );

     int nodecount = 0;

     int w = int(canvas.width());
     int h = int(canvas.height());

     const int dist = 30;
     int rows = h / dist;
     int cols = w / dist;

 #ifndef QT_NO_PROGRESSDIALOG
     Q3ProgressDialog progress( &quot;Creating mesh...&quot;, &quot;Abort&quot;, rows,
                               this, &quot;progress&quot;, TRUE );
 #endif

     Q3MemArray&lt;NodeItem*&gt; lastRow(cols);
     for ( int j = 0; j &lt; rows; j++ ) {
         int n = j%2 ? cols-1 : cols;
         NodeItem *prev = 0;
         for ( int i = 0; i &lt; n; i++ ) {
             NodeItem *el = new NodeItem;
             canvas.addItem(el);
             nodecount++;
             int r = qrand();
             int xrand = r %20;
             int yrand = (r/20) %20;
             el-&gt;setPos( xrand + x0 + i*dist + (j%2 ? dist/2 : 0 ),
                         yrand + y0 + j*dist );

             if ( j &gt; 0 ) {
                 if ( i &lt; cols-1 )
                     canvas.addItem(new EdgeItem( lastRow[i], el));
                 if ( j%2 )
                     canvas.addItem(new EdgeItem( lastRow[i+1], el));
                 else if ( i &gt; 0 )
                     canvas.addItem(new EdgeItem( lastRow[i-1], el));
             }
             if ( prev ) {
                 canvas.addItem(new EdgeItem( prev, el));
             }
             if ( i &gt; 0 ) lastRow[i-1] = prev;
             prev = el;
         }
         lastRow[n-1]=prev;
 #ifndef QT_NO_PROGRESSDIALOG
         progress.setProgress( j );
         if ( progress.wasCancelled() )
             break;
 #endif
     }
 #ifndef QT_NO_PROGRESSDIALOG
     progress.setProgress( rows );
 #endif
     <span class="comment">//</span> qDebug( &quot;%d nodes, %d edges&quot;, nodecount, EdgeItem::count() );
 }

 void Main::addRectangle()
 {
     QAbstractGraphicsShapeItem *i = canvas.addRect( QRectF(qrand()%int(canvas.width()),
                                                           qrand()%int(canvas.height()),
                                                           canvas.width()/5,
                                                           canvas.width()/5) );
     i-&gt;setFlag(QGraphicsItem::ItemIsMovable);
     int z = qrand()%256;
     i-&gt;setBrush( QColor(z,z,z) );
     i-&gt;setPen( QPen(QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8), 6) );
     i-&gt;setZValue(z);
 }</pre>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>
</tr></table></div></address></body>
</html>
